﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.ServiceModel.Syndication;
using System.Xml.Linq;
using System.Linq;

namespace PicasaManager
{
    public class GooglePicasaUser
    {
        public GooglePicasaUser()
        {
            Albums = new List<GoogleAlbum>();
        }

        public List<GoogleAlbum> Albums { get; set; }

        public string Id { get; set; }
        public DateTime Updated { get; set; }
        public string Title { get; set; }
        public string IconUrl { get; set; }
        public string AuthorName { get; set; }
        public string AuthorUrl { get; set; }
        public string PhotoUser { get; set; }
        public string PhotoNickname { get; set; }
        public string PhotoThumbnail { get; set; }

        public static GooglePicasaUser FromXmlStream(Stream xmlStream)
        {
            GooglePicasaUser user = new GooglePicasaUser();
            XmlReader responseReader = XmlReader.Create(xmlStream);
            SyndicationFeed feed = SyndicationFeed.Load(responseReader);

            user.Id = feed.Id;
            user.Updated = feed.LastUpdatedTime.DateTime;
            user.Title = feed.Title.Text;
            //user.IconUrl = feed.
            user.AuthorName = feed.Authors[0].Name;
            user.AuthorUrl = feed.Authors[0].Uri;

            user.IconUrl = feed.ElementExtensions.Where(ex => ex.OuterNamespace == "http://www.w3.org/2005/Atom").FirstOrDefault().GetObject<XElement>().Value;

            foreach (var ex in feed.ElementExtensions.Where(ex => ex.OuterNamespace == "http://schemas.google.com/photos/2007"))
            {
                var element = ex.GetObject<XElement>();
                switch (ex.OuterName)
                {
                    case "user": user.PhotoUser = element.Value; break;
                    case "nickname": user.PhotoNickname = element.Value; break;
                    case "thumbnail": user.PhotoThumbnail = element.Value; break;
                }
            }
            foreach (var item in feed.Items)
            {
                GoogleAlbum album = new GoogleAlbum();
                album.Updated = item.LastUpdatedTime.DateTime;
                album.Published = item.PublishDate.DateTime;
                album.Title = item.Title.Text;
                album.Summary = item.Summary.Text;
                album.Author = item.Authors[0].Name;
                album.AuthorUri = item.Authors[0].Uri;

                XElement mediaGroup = item.ElementExtensions.Where(ex => ex.OuterNamespace == "http://search.yahoo.com/mrss/").FirstOrDefault().GetObject<XElement>();

                foreach(var descendant  in mediaGroup.Descendants())
                {
                    switch (descendant.Name.LocalName)
                    {
                        case "content": album.Media.Contents.Add(GoogleMediaContent.FromXElement(descendant)); break;
                        case "credit": album.Media.Credit = descendant.Value; break;
                        case "description": album.Media.Description = descendant.Value; break;
                        case "keywords": album.Media.Description = descendant.Value; break;
                        case "thumbnail": album.Media.Thumbnails.Add(GoogleMediaThumbnail.FromXElement(descendant)); break;
                        case "title": album.Media.Title = descendant.Value; break;
                    }
                }
                 

                foreach (var ex in item.ElementExtensions.Where(ex => ex.OuterNamespace == "http://schemas.google.com/photos/2007"))
                {
                    var element = ex.GetObject<XElement>();
                    switch (ex.OuterName)
                    {
                        case "id": album.Id = element.Value; break;
                        case "name": break;
                        case "location": break;
                        case "access": break;
                        case "timestamp":  break;
                        case "numphotos": album.PhotoCount = Convert.ToInt32(element.Value); break;
                        case "user": album.Author = element.Value; break;
                        case "nickname":   break;
                        case "commentingEnabled": album.CommentsEnabled = element.Value!="0"; break;
                        case "commentCount": album.CommentCount = Convert.ToInt32(element.Value); break;
                    }
                }
                user.Albums.Add(album);
            }
            return user;
        }

    }
}
